package com.fuelwallet.view;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.text.Editable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;

import com.fuelwallet.R;
import com.fuelwallet.model.MySuggestionProvider;
import com.fuelwallet.model.SearchedPlacesHistoryHandle;

public class ObtainDirectionActivity extends Activity {
	// Debugging
    private static final String TAG = "ObtainDirectionActivity";
    private static final boolean D = true;
    
    // Result Intent Key Names
    public static final String FROM = "from";
    public static final String TO = "to";
    
    private AutoCompleteTextView mFrom;
    private AutoCompleteTextView mTo;
    private Button mGetDirection;
    private ImageButton mExchange;
    
    private SearchedPlacesHistoryHandle searchHistoryHandle;
    private List<String> place_history;
       
    protected void onCreate(Bundle savedInstanceState){
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.obtain_direction);
    	
    	mFrom = (AutoCompleteTextView) findViewById(R.id.autotext_from);
    	mTo = (AutoCompleteTextView) findViewById(R.id.autotext_to);
    	mGetDirection = (Button) findViewById(R.id.btn_get_direction);
    	mExchange = (ImageButton) findViewById(R.id.btn_exchange);
    	
    	searchHistoryHandle = new SearchedPlacesHistoryHandle(this);
    	
    	mGetDirection.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {				
				if(mFrom.getText().length()==0 || mTo.getText().length()==0)
					return;
				
				String _from = mFrom.getText().toString();
				String _to = mTo.getText().toString();
				
				// update history
				searchHistoryHandle.open();
	    			searchHistoryHandle.putPlace(_from);
	    			searchHistoryHandle.putPlace(_to);
	    		searchHistoryHandle.close();
	    		SearchRecentSuggestions suggestions = new SearchRecentSuggestions(ObtainDirectionActivity.this,
	                    MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
	    		if(!_from.toLowerCase().equals("my location"))
	    			suggestions.saveRecentQuery(_from, null);
	    		if(!_to.toLowerCase().equals("my location"))
	    			suggestions.saveRecentQuery(_to, null);
				
				Intent intent = new Intent();
				intent.putExtra(FROM, _from);
				intent.putExtra(TO, _to);
				setResult(Activity.RESULT_OK, intent);
				
				ObtainDirectionActivity.this.finish();
			}
		});
    	mExchange.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				Editable _from = mFrom.getText();
				Editable _to = mTo.getText();
				
				mFrom.setText(_to);
				mTo.setText(_from);
			}
		});
    }
    
    protected void onStart(){
    	Intent intent = getIntent();    	
    	String passed_from = intent.getStringExtra(FROM);
    	String passed_to = intent.getStringExtra(TO);
    	
    	if(passed_from != null)
    		mFrom.setText(passed_from);
    	if(passed_to != null)
    		mTo.setText(passed_to);
    	
    	if(mFrom.length() == 0 && mTo.length() == 0){
    		mFrom.setText("My Location");
    		mTo.setText("");
    	}
    	
    	searchHistoryHandle.open();
    		place_history = searchHistoryHandle.getPlaceHistory();
    	searchHistoryHandle.close();
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, place_history);
    	mFrom.setAdapter(adapter);
    	mTo.setAdapter(adapter);
    	   	   	
    	super.onStart();
    }
}
